package tk.kolesnikov.tests;

import tk.kolesnikov.algorithms.HammingHelper;
import tk.kolesnikov.hashes.AHash;
import tk.kolesnikov.hashes.ImageHash;
import tk.kolesnikov.hashes.ImagePHash;
import tk.kolesnikov.hashes.PHash;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * Created by seko0313 on 16.01.14.
 */
public class ImageUtilsTest {

    public static void main(String args[]) throws Exception {

        test1();
        test2();
        /*BufferedImage image = ImageIO.read(new File("images" + File.separator + "test.png"));
        BufferedImage image1 = ImageIO.read(new File("images" + File.separator + "reduced_image1.png"));

        ImageHash hash = new AHash();
        String hash1 = hash.getHash(image);
        String hash2 = hash.getHash(image1);

        int distance = HammingHelper.getHammingDistance(hash1, hash2);
        System.out.println("distance: " + distance + "; percentage: " + (distance*100)/(32*32));
         */

        /*int[] averageColor = imageUtils.getAverageColors(image);
        for (int i = 0; i < averageColor.length; i++)
            System.out.println(averageColor[i]);

        int[][] mask = imageUtils.getAverageMask(image, averageColor[1]);
        System.out.println(imageUtils.getAverageMask(image, averageColor[1]));



        System.out.println(imageUtils.averageMaskToHash(mask));*/

        //File outputfile = new File("reduced_image1.png");
        //ImageIO.write(imageUtils.resize(image, 32, 32), "png", outputfile);

        //outputfile = new File("greyscale1.png");
        //ImageIO.write(imageUtils.graysсale(image), "png", outputfile);
    }

    private static void test1() throws IOException {
        BufferedImage image = ImageIO.read(new File("images" + File.separator + "3a.jpg"));
        BufferedImage image1 = ImageIO.read(new File("images" + File.separator + "3b.jpg"));

        ImageHash hash = new AHash();
        String hash1 = hash.getHash(image);
        String hash2 = hash.getHash(image1);
        System.out.println(hash1.length());
        int distance = HammingHelper.getHammingDistance(hash1, hash2);
        System.out.println("aHash => distance: " + distance + "; percentage: " + (distance*100)/(8*8));

        hash = new PHash();
        hash1 = hash.getHash(image);
        hash2 = hash.getHash(image1);
        System.out.println(hash1.length());
        distance = HammingHelper.getHammingDistance(hash1, hash2);
        System.out.println("pHash => distance: " + distance + "; percentage: " + (distance*100)/(8*8));
    }

    private static void test2() throws Exception {
        ImagePHash imagePHash = new ImagePHash();
        FileInputStream fis = new FileInputStream(new File("images" + File.separator + "3a.jpg"));
        FileInputStream fis1 = new FileInputStream(new File("images" + File.separator + "3b.jpg"));

        String hash = imagePHash.getHash(fis);
        String hash1 = imagePHash.getHash(fis1);
        System.out.println(hash.length());
        int distance = imagePHash.distance(hash, hash1);
        System.out.println("distance: " + distance + "; percentage: " + distance*100/hash.length());
    }

}
